<?php

namespace Admin\Controller;

use Common\Lib\Tree;

class MenuController extends AdminController {

    public function index() {
        $result = M('AdminMenu')->order('pid ASC,sort ASC,id DESC')->select();
        foreach ($result as $key => $val) {
            $result[$key]['add'] = '<a href="' . U('menu/add', ['pid' => $val['id']]) . '" class=" tooltip-info ajax-load" data-toggle="tooltip" data-placement="right" data-original-title="添加子菜单"><i class="fa fa-plus"></i></a>';
            if ($val['status'] == 1) {
                $result[$key]['status'] = '<div class="btn-group">
	                                        <a class="btn btn-sm btn-info">显示</a>
	                                        <a class="btn btn-sm btn-info dropdown-toggle" data-toggle="dropdown" aria-expanded="false"><i class="fa fa-angle-down"></i></a>
	                                        <ul class="dropdown-menu dropdown-info" role="menu">
	                                            <li>
	                                                <a href="' . U('menu/status', ['id' => $val['id'], 'status' => 0]) . '">隐藏</a>
	                                            </li>
	                                        </ul>
	                                    </div>';
            } else {
                $result[$key]['status'] = '<div class="btn-group">
	                                        <a class="btn btn-sm btn-darkorange">隐藏</a>
	                                        <a class="btn btn-sm btn-darkorange dropdown-toggle" data-toggle="dropdown" aria-expanded="false"><i class="fa fa-angle-down"></i></a>
	                                        <ul class="dropdown-menu dropdown-darkorange" role="menu">
	                                            <li>
	                                                <a href="' . U('menu/status', ['id' => $val['id'], 'status' => 1]) . '">显示</a>
	                                            </li>
	                                        </ul>
	                                    </div>';
            }
            $result[$key]['delete_link'] = U('menu/delete', ['id' => $val['id']]);
            $result[$key]['edit_link'] = U('menu/edit', ['id' => $val['id']]);
            $result[$key]['auth_str'] = $val['auth'] == 1 ? '<span class="success"><i class="glyphicon glyphicon-ok"></i></span>' : '<span class="danger"><i class="glyphicon glyphicon-remove"></i></span>';
        }
        $Tree = new Tree($result);
        $Tree->icon = array("&nbsp;&nbsp;&nbsp;&nbsp; ", "&nbsp;&nbsp;&nbsp;├─ ", "&nbsp;&nbsp;&nbsp;└─ ");
        $Tree->nbsp = "&nbsp;&nbsp;&nbsp;";
        $html = "<tr>                    
                    <td width='80' class='input-group-xs'><input type='text' name='sort[\$id]' style='text-align:center' value='\$sort' class='form-control' /></td>
                    <td class='left'><span class='spacer'>\$spacer</span><i class='\$icon'></i> \$name \$add</td>
                    <td class='text-align-center'>\$href</td>
                    <td class='text-align-center'>\$auth_str</td>
                    <td class='text-align-center'>\$status</td>
                    <td class='text-align-center'><a href='\$edit_link' class='ajax-load'><i class='fa fa-edit'></i> 编辑</a> <a href='\$delete_link'  class='margin-left-10 ajax-link' data-confirm='确定删除该菜单？'><i class='fa fa-trash-o'></i> 删除</a></td>
            </tr>";

        $result = $Tree->get_tree(0, $html);
        $this->assign('result', $result);
        $this->display();
    }

    public function add() {
        if (IS_POST) {
            $name = I('name');
            $href = I('href');
            $pid = I('pid',0,'intval');
            $icon = I('icon');
            $auth = I('auth',1,'tinyintval');
            if (!$name) {
                $this->error('菜单名称不能为空');
            }
            $data = [
                'name' => $name,
                'href' => $href,
                'pid' => $pid,
                'icon' => $icon,
                'auth'=>$auth,
            ];
            M('AdminMenu')->data($data)->add();
            $this->success('添加成功');
        } else {
            $pid = I('pid', 0, 'intval');
            $this->assign('treeList', $this->treeMenu($pid));
            $this->display();
        }
    }

    public function edit() {
        $id = I('id', 0, 'intval');
        if (IS_POST) {
            $name = I('name');
            $href = I('href');
            $pid = I('pid',0,'intval');
            $icon = I('icon');
             $auth = I('auth',1,'tinyintval');
            if (!$name) {
                $this->error('菜单名称不能为空');
            }
            $data = [
                'name' => $name,
                'href' => $href,
                'pid' => $pid,
                'icon' => $icon,
                'auth'=>$auth,
            ];
            M('AdminMenu')->where(['id'=>$id])->data($data)->save();
            $this->success('添加成功');
        } else {
            $result = M('AdminMenu')->find($id);
            $this->assign($result);
            $this->assign('treeList', $this->treeMenu($result['pid']));
            $this->display();
        }
    }

    public function icon() {
        $this->display();
    }
    
    public function sort(){ 
        $sorts = I('sort',0,'intval');
        foreach($sorts as $key=>$val){ 
            M('AdminMenu')->where(['id'=>$key])->setField('sort',$val);            
        }
        $this->success('操作成功');
    }

    public function status() {
        $id = I('id', 0, 'intval');
        $status = I('status', 1, 'tinyintval');
        M('AdminMenu')->where(['id' => $id])->setField('status', $status);
        $this->success('操作成功');
    }

    public function delete() {
        M('AdminMenu')->where(['id' => I('id', 0, 'intval')])->delete();
        $this->success('编辑成功');
    }

    public function treeMenu($selected = 0) {
        $result = M('AdminMenu')->order('pid ASC,id DESC')->where(['auth'=>1])->select();
        $Tree = new Tree($result);
        $html = "<option value='\$id' \$selected>\$spacer \$name</option>";
        $result = $Tree->get_tree(0, $html, $selected,'&nbsp;&nbsp;&nbsp;&nbsp;');
        return $result;
    }

}
